package com.badibadi.activity;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.json.JSONException;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import com.badibadi.adapter.AddingRecordNewAdapter;
import com.badibadi.infos.ClueTypeModel;
import com.badibadi.infos.ListsModel;
import com.badibadi.infos.Results;
import com.badibadi.infos.UserInfoModel;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.Dialog;
import com.badibadi.mytools.InterestModel;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.StringUtils;
import com.badibadi.mytools.UserDataSet;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;

public class InterestSelectActivity extends BaseActivity {

	private static final String TAG = "InterestSelectActivity";// insterest_finish
	private GridView gv;
	private AddingRecordNewAdapter arnAdapter;
	private Button clear_text;
	private EditText et0;
	private Map<Integer, String> TitleMap;

	private UserInfoModel userInfoModel;
	private UserDataSet userSet;
	private List<InterestModel> Interestlist;
	private String typeid = null;
	private String[] type_id;
	private ListsModel listsModel;
	String languageType;
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(InterestSelectActivity.this);
				Utils.showMessage(InterestSelectActivity.this, getResources()
						.getString(R.string.l_net_error));
				break;
			case 2:
				Utils.ExitPrgress(InterestSelectActivity.this);
				initGridView();

				break;
			case 3:
				Utils.ExitPrgress(InterestSelectActivity.this);
				Utils.showMessage(InterestSelectActivity.this, getResources()
						.getString(R.string.l_xa10));
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.activity_interest_select);
		
		languageType=Dialog
		.getSystemLanguageType(InterestSelectActivity.this);
		typeid = getIntent().getStringExtra("typeid");
		if (typeid != null) {
			type_id = typeid.split("\\,");
		}
		// for(int i=0;i<type_id.length;i++)
		// {
		//    
		// }

		this.findViewById(R.id.return_btn).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Intent intent = new Intent(InterestSelectActivity.this,
								MyDataActivity.class);
						startActivity(intent);
						overridePendingTransition(R.anim.slide_left2,
								R.anim.slide_right2);
						finish();
					}
				});

		userSet = new UserDataSet(InterestSelectActivity.this);

		if (getIntent().getSerializableExtra("MyDataActivity") != null)
			userInfoModel = (UserInfoModel) getIntent().getSerializableExtra(
					"MyDataActivity");
		// initGridView();
		Interestlist = new ArrayList<InterestModel>();
		// for(int i=0;i<type_id.length;i++)
		// {
		// InterestModel in=new InterestModel();
		// in.setId(type_id[i]);
		// Interestlist.add(in);
		// }
		// Interestlist.add(object)
		Interest();
	}

	/**
	 * 这里是gridVe
	 */
	private void initGridView() {
		// TODO Auto-generated method stub
		if (Interestlist != null) {
			Interestlist.clear();
		}
		if (listsModel.getLists() != null && !listsModel.getLists().isEmpty()) {
			Interestlist.addAll(listsModel.getLists());
		}
		gv = (GridView) this.findViewById(R.id.interest_select_gridView1);

		arnAdapter = new AddingRecordNewAdapter(InterestSelectActivity.this,
				Interestlist);

		TitleMap = new Hashtable<Integer, String>();
		gv.setAdapter(arnAdapter);

		// for(int i=0;i<type_id.length;i++)
		// {
		// InterestModel in=new InterestModel();
		// in.setId(type_id[i]);
		// Interestlist.add(in);
		// }

		if(type_id!=null)
		{
			for (int i = 0; i < type_id.length; i++) {
				arnAdapter.setIsItemClick_id(type_id[i]);
			}
		}
	
		arnAdapter.notifyDataSetChanged();
		gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				if (arnAdapter.getIsItemClick(position) == true) {
					arnAdapter.setIsItemClick(position);
					arnAdapter.notifyDataSetInvalidated();
				} else if (arnAdapter.getSelectNumber() >= arnAdapter
						.getUpperLimit()) {
					ShowToast(InterestSelectActivity.this,
							getResources().getString(R.string.l_xb77)
									+ arnAdapter.getUpperLimit()
									+ getResources().getString(R.string.l_xb5));
				} else {
					// ShowToast(InterestSelectActivity.this,
					// "你点击了" + arnAdapter.getItem(position));
					arnAdapter.setIsItemClick(position);
					arnAdapter.notifyDataSetInvalidated();
				}
				// 在提交按钮将TitleMap提交
				TitleMap = arnAdapter.getTitleMap();
//				   

			}
		});

		this.findViewById(R.id.insterest_finish).setOnClickListener(
				new View.OnClickListener() {

					List<String> mlist = new ArrayList<String>();

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (userInfoModel != null) {
							for (Map.Entry<Integer, String> entry : TitleMap
									.entrySet()) {
//								   ("key= " + entry.getKey()
//										+ " and value= " + entry.getValue());
								mlist.add(entry.getValue());
							}
							String str = "";
							//    
							if (mlist != null) {

								for (int i = 0; i < mlist.size(); i++) {
									if (i == 0) {
										str = mlist.get(i);
									} else {
										str = str + "," + mlist.get(i);
									}
								}
							}
//							   

							userInfoModel.setTypeid(str);
							str = "";
							userSet.UserDataSet(userInfoModel, 0, "", true);
						} else {
							ShowToast(getApplicationContext(), getResources()
									.getString(R.string.l_net_error));
						}
					}
				});

	}

	private void Interest() {
		Utils.showPrgress(InterestSelectActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				listsModel = new ListsModel();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("type", "xingqu");
				map.put("languageType", languageType);

				String content = Utils.sendRequest(map, Constants.BadiUrl_1
						+ "type/listType");
				if (content != null) {
					//    ("结果是:" + content);
					Results results = Utils.checkResult_NNN(
							getApplicationContext(), content);
					if (results != null && results.getRetmsg() != null) {
//						   
						try {
							listsModel = JSONUtils.getEntityByJsonString(
									results.getRetmsg(), ListsModel.class);
							handler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
//							   
							   
						}
					} else {
						handler.sendEmptyMessage(3);
					}
				} else {
					handler.sendEmptyMessage(1);
				}
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		super.onResume();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub

		super.onDestroy();
	}

	//    
	// String content = Utils.sendRequest(map, Constants.BadiUrl
	// + "userDataSet");
	//    
	//    
	//    ("content-->" + content);
	// if (content != null) {
	// //    ("结果是:" + content);
	// Results results = Utils.checkResult_NNN(getApplicationContext(),
	// content);
	//
	// if (results != null && results.getRetmsg()!=null) {
	// // results.getRetmsg()
	// // != null
	// //    
	// // "得到的结果是："
	// // results.getRetmsg());
	// Toast.makeText(getApplicationContext(), results.getRetmsg(), 1)
	// .show();
	//    
	// try {
	// Thread.sleep(3000);
	// finish();
	// } catch (Exception e) {
	// // TODO: handle exception
	// }
	//
	//
	// // userInfoModel = JsonUtil.getEntityByJsonString(
	// // results.getRetmsg(), UserInfoModel.class);
	// // ImageView iv=(ImageView)this. // TextOrimg_Id[0].
	// // RefreshData();// 更新数据
	//
	// } else {
	// Toast.makeText(getApplicationContext(), "输出结果为空", 1).show();
	//    
	// }
	// } else {
	//
	// Utils.ExitPrgress(InterestSelectActivity.this);
	// ShowToast(InterestSelectActivity.this, "网络连接出错");
	// }

}
